package com.uzhie.web.service;

import com.uzhie.web.bean.PageBean;
import com.uzhie.web.dao.CatalogDAO;
import com.uzhie.web.dao.VehicleDAO;
import com.uzhie.exception.UserException;
import com.uzhie.exception.VehicleException;
import com.uzhie.web.module.Vehicle;
import com.uzhie.util.ConstantKit;
import org.apache.ibatis.session.RowBounds;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户service,封装与用户相关的服务操作
 * <p>
 * Created by lilongfei on 2018/7/12.
 */
@Service
public class VehicleService {
    @Autowired
    private VehicleDAO vehicleDAO;
    @Autowired
    private CatalogDAO catalogDAO;

    /**
     * 根据搜索条件检索各个车辆信息
     *
     * @param departName 部门名称
     * @param name       车辆名称
     * @param type       车辆类型
     * @param mfrsName   生产厂商
     * @param number     车牌号
     * @param simNumber  sim卡号
     * @return
     */
    public List<Vehicle> selectVehicleByCondition(String departName, String name, String type, String mfrsName, String number, String simNumber, PageBean pageBean) {
        return vehicleDAO.selectVehicleByCondition(departName, name, type, mfrsName, number, simNumber, pageBean.getRowBounds());
    }

    /**
     * 获取可以进行绑定的车辆信息
     *
     * @param departmentId
     * @param type
     * @param vehicleNumber
     * @param rowBounds
     * @return
     * @throws UserException
     */
    public List<Vehicle> selectVehicleToBind(@NotEmpty(message = UserException.Status.DEPT_ISNULL) String departmentId, String type, String vehicleNumber, RowBounds rowBounds) throws UserException {


        return vehicleDAO.selectVehicleToBind(departmentId, type, vehicleNumber, rowBounds);
    }

    /**
     * 新增车辆
     *
     * @param vehicle 车辆信息
     */
    public void addVehicle(Vehicle vehicle) throws VehicleException {
        //更新服务器时间
        try {
            vehicle.setCreateTime(new Date());
            vehicle.setStatus(ConstantKit.STATUS.IS_AVAILABLE);
            vehicle.setName(vehicle.getNumber());
            if (vehicle.getNumber() != null)
                if (vehicleDAO.isExist(vehicle.getNumber()))
                    throw new VehicleException(VehicleException.Status.VEHICLE_IS_EXIST);
            int t = vehicleDAO.insert(vehicle);
            if (t != 1) throw new VehicleException(VehicleException.Status.INSERT_ERROR);
        } catch (Exception e) {
            throw new VehicleException(VehicleException.Status.INSERT_ERROR);
        }
    }

    /**
     * 根据车辆id,获取车辆详情
     *
     * @param vehicleId 车辆id
     * @return
     */
    public Vehicle selectVehicleById(String vehicleId) {
        Vehicle vehicle = vehicleDAO.selectVehicleById(vehicleId);
        return vehicle;
    }

    /**
     * 编辑车辆信息
     *
     * @param vehicle 车辆信息
     */
    public void updateVehicle(Vehicle vehicle) throws VehicleException {
        //更新服务器时间
        vehicle.setUpdateTime(new Date());
        if (vehicle.getStatus() == 0)
            vehicleDAO.deleteByPrimaryKey(vehicle.getId());
        else {
            int t = vehicleDAO.updateByPrimaryKeySelective(vehicle);
            if (t != 1) throw new VehicleException(VehicleException.Status.UPDATA_ERROR);
        }
    }


    /**
     * 根据角色id,批量修改车辆信息数据库状态
     *
     * @param vehicleIds 车辆id集合
     * @param status     状态
     */
    public void updateVehicleStatusById(List<Vehicle> vehicleIds, Integer status) throws VehicleException {
        if (vehicleIds.size() == 0) throw new VehicleException(VehicleException.Status.ID_ISNULL);
        for (Vehicle vehicle : vehicleIds) {
            if (status == 0)
                vehicleDAO.deleteByPrimaryKey(vehicle.getId());
            else {
                int t = vehicleDAO.updateVehicleStatusById(vehicle.getId(), status);
                if (t == 0) throw new VehicleException(VehicleException.Status.UPDATA_STATUS_ERROR);
            }
        }
    }

    /**
     * 通过车牌号查询车辆信息
     *
     * @param number 车牌号
     * @return 如果存在车辆，则返回车辆信息，如果不存在，则返回null
     */
    public Vehicle selectByNumber(@NotEmpty(message = VehicleException.Status.VEHICLE_NUMBER_ISNULL) String number) {
        return vehicleDAO.selectVehicleNumber(number);
    }
}


